<?php 
defined('IN_TS') or die('Access Denied.');

//用户是否登录
$userid = aac('user')->isLogin();

switch($ts){	

	case "":

        if ($TS_APP['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
            tsNotice('课程设置不允许会员发课程！');
        }
	
		//课程分类
		$arrCates = $new['study']->findAll('study_cates',array(
            'recateid'=>0
        ),'cateid asc');
		
		$title = '创建课程';
		include template("create");
		break;
	
	
	//执行创建课程
	case "do":
			
		$studyname = trim($_POST['studyname']);
		$studydesc = tsCleanContent($_POST['studydesc']);
		
		$price = floatval($_POST['price']);
		
		$cateid = intval($_POST['cateid']);
		$cateid2 = intval($_POST['cateid2']);
		
		if($studyname=='' || $studydesc=='' || $cateid=='') {
			tsNotice('所有必填项都不能为空！');
		}


        $isStudy = $new['study']->findCount('study',array(
            'studyname'=>$studyname,
        ));

        if($isStudy>0){
            tsNotice('课程名称已经存在！');
        }


		if (intval ( $TS_USER ['isadmin'] ) == 0) {
			// 过滤内容开始
			aac ( 'system' )->antiWord ( $studyname );
			aac ( 'system' )->antiWord ( $studydesc );
			// 过滤内容结束
		}


        //1审核后显示0不审核
        if ($TS_APP['isaudit'] == 1) {
            $isaudit = 1;
        } else {
            $isaudit = 0;
        }
		
		//创建课程
		$studyid = $new['study']->create('study',array(
			
			'userid'=>$userid,
			'studyname'=>$studyname,
			'studydesc'=>$studydesc,
			
			'price'=>$price,
			
			'cateid'=>$cateid,
			'cateid2'=>$cateid2,
			'ispublish'=>1,    //未发布状态
			'isaudit'=>$isaudit,
			'addtime'=>time(),
			'uptime'=>time(),
		
		));
		
		//同时把自己加入学习课程状态中study_learn
        if($studyid){
            $new['study']->create('study_learn',array(
                'userid'=>$userid,
                'studyid'=>$studyid,
                'status'=>1,
                'addtime'=>time(),
            ));

            header("Location: ".tsUrl('study','show',array('id'=>$studyid)));
        }else{
            tsNotice('课程创建失败！');
        }


		break;
	}
	
